//
//  AddAndEditQuickRoomView.m
//  QRMaster
//
//  Created by myApple on 16/5/30.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "AddAndEditQuickRoomView.h"
#import "UIImageView+WebCache.h"

@interface AddAndEditQuickRoomView()
{
    UITextField * cardNameTextField;
    UILabel * roomNameLabel;
}

@end

@implementation AddAndEditQuickRoomView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self prepareView];
    }
    
    return self;
}

-(void)prepareView
{
    //房卡名称
    float x = 0;
    float y = 0;
    float width = self.frame.size.width ;
    float height = 39;
    UIView * cardTitleView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:cardTitleView];
    
    x = 16;
    y = 15;
    width = UI_SCREEN_WIDTH - x;
    height = 20;
    UILabel * cardNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    cardNameLabel.text = NSLocalizedString(@"roomName", nil);
    cardNameLabel.font = [UIFont systemFontOfSize:14.0];
    [cardTitleView addSubview:cardNameLabel];
    
    y = cardTitleView.frame.origin.y + cardTitleView.frame.size.height;
    x = 0;
    height = 0.5;
    width = UI_SCREEN_WIDTH;
    UIView * cardLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    cardLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [self addSubview:cardLineView];
    
    x = 0;
    y += height;
    height = 44;
    UIView * addCardNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addCardNameView.backgroundColor = [UIColor whiteColor];
    [self addSubview:addCardNameView];
    
    x = 22;
    y = 0;
    width = UI_SCREEN_WIDTH - x;
    height = 44;
    cardNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    cardNameTextField.placeholder = NSLocalizedString(@"addRoomName",@"添加房卡名称");
    cardNameTextField.font = [UIFont systemFontOfSize:15.0];
    cardNameTextField.clearButtonMode = UITextFieldViewModeAlways;
    [cardNameTextField addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    [addCardNameView addSubview:cardNameTextField];
    
    y = 43.5;
    x = 0;
    height = 0.5;
    width = UI_SCREEN_WIDTH;
    UIView * textFieldLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    textFieldLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [addCardNameView addSubview:textFieldLineView];
    
    //房间
    y = addCardNameView.frame.origin.y + addCardNameView.frame.size.height;
    x = 0;
    height = 34;
    width = UI_SCREEN_WIDTH;
    UIView * roomView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:roomView];
    
    x = 16;
    y = 10;
    width = UI_SCREEN_WIDTH - x;
    height = 20;
    UILabel * roomLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLabel.text = NSLocalizedString(@"room", nil);
    roomLabel.font = [UIFont systemFontOfSize:14.0];
    [roomView addSubview:roomLabel];
    
    y = roomView.frame.origin.y + roomView.frame.size.height;
    height = 0.5;
    width = UI_SCREEN_WIDTH;
    x = 0;
    UIView * roomLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [self addSubview:roomLineView];
    
    x = 0;
    y += height ;
    height = 44;
    UIButton * addroomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addroomBtn.frame = CGRectMake(x, y, width, height);
    [addroomBtn addTarget:self action:@selector(addRoom) forControlEvents:UIControlEventTouchUpInside];
    [addroomBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:addroomBtn];
    
    x = 22;
    y = 12;
    width = UI_SCREEN_WIDTH - x;
    height = 20;
    roomNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    roomNameLabel.text = NSLocalizedString(@"chooseRoomQuick", @"通过列表选择房间");
    roomNameLabel.textColor = UIColorFromRGB(0xc1c1c1);
    roomNameLabel.font = [UIFont systemFontOfSize:15.0];
    [addroomBtn addSubview:roomNameLabel];
    
    x = UI_SCREEN_WIDTH - 24;
    y = 16;
    width = 8;
    height = 12;
    UIImageView * nextImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nextImgView.image = [UIImage imageNamed:@"nextIndicate"];
    [addroomBtn addSubview:nextImgView];
    
    y = 43.5;
    x = 0;
    height = 0.5;
    width = UI_SCREEN_WIDTH;
    UIView * btnLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    btnLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [addroomBtn addSubview:btnLineView];
    
    //背景图片
    y = addroomBtn.frame.size.height + addroomBtn.frame.origin.y;
    x = 0;
    height = 34;
    width = UI_SCREEN_WIDTH;
    UIView * imageBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:imageBackView];
    
    x = 16;
    y = 10;
    width = UI_SCREEN_WIDTH - x;
    height = 20;
    UILabel * imageLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    imageLabel.text = NSLocalizedString(@"backView", nil);
    imageLabel.font = [UIFont systemFontOfSize:14.0];
    [imageBackView addSubview:imageLabel];
    
    y = imageBackView.frame.origin.y + imageBackView.frame.size.height;
    height = 0.5;
    x = 0;
    width = UI_SCREEN_WIDTH;
    UIView * imageLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    imageLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [self addSubview:imageLineView];
    
    x = 0;
    y += height;
    height = 96;
    width = UI_SCREEN_WIDTH;
    UIView * addImgView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addImgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:addImgView];
    
    x = 16;
    y = 12;
    width = 146;
    height = 72.5;
    _roomImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    _roomImgView.backgroundColor = [UIColor clearColor];
    _roomImgView.userInteractionEnabled = YES;
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = UIColorFromRGB(0xc1c1c1).CGColor;
    border.fillColor = nil;
    border.path = [UIBezierPath bezierPathWithRect:_roomImgView.bounds].CGPath;
    border.frame = _roomImgView.bounds;
    border.lineWidth = 0.5;
    border.lineCap = @"square";
    border.lineDashPattern = @[@4,@4];
    [_roomImgView.layer addSublayer:border];
    [addImgView addSubview:_roomImgView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addImgTap)];
    [_roomImgView addGestureRecognizer:tap];
    
    x = 16;
    y = addImgView.frame.size.height / 2.0 - 10;
    height = 20;
    UILabel * addImgLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addImgLabel.text = NSLocalizedString(@"addImage", @"添加图片");
    addImgLabel.textColor = UIColorFromRGB(0x727272);
    addImgLabel.font = [UIFont systemFontOfSize:15.0];
    addImgLabel.textAlignment = NSTextAlignmentCenter;
    [addImgView insertSubview:addImgLabel belowSubview:_roomImgView];
    
    
    y = addImgView.frame.size.height - 0.5;
    x = 0;
    height = 0.5;
    width = UI_SCREEN_WIDTH;
    UIView * addImgLineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addImgLineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [addImgView addSubview:addImgLineView];
}

-(void)setModel:(QuickCardModel *)model
{
    if (model.roomName.length > 0) {
        roomNameLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@",model.roomName,model.floorName,model.buildingName,model.communityName];
        roomNameLabel.textColor = UIColorFromRGB(0x7dc234);
    }
    else
    {
        roomNameLabel.textColor = UIColorFromRGB(0xc1c1c1);
    }
    cardNameTextField.text = model.customerName;
//    [_roomImgView sd_setImageWithURL:[NSURL URLWithString:model.imageURL]];
    
}

#pragma mark - 修改房卡名称
-(void)textDidChange:(UITextField *)textInput
{
    if (_delegate && [_delegate respondsToSelector:@selector(AddAndEditQuickRoomViewChangeName:)]) {
        [_delegate AddAndEditQuickRoomViewChangeName:textInput.text];
    }
}

#pragma mark - 添加房间
-(void)addRoom
{
    if (_delegate && [_delegate respondsToSelector:@selector(AddAndEditQuickRoomViewAddRoom)]) {
        [_delegate AddAndEditQuickRoomViewAddRoom];
    }
}

#pragma mark - 点击添加
-(void)addImgTap
{
    if (_delegate && [_delegate respondsToSelector:@selector(AddAndEditQuickRoomViewChangeImage)]) {
        [_delegate AddAndEditQuickRoomViewChangeImage];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
